package com.shuoyue.ycgk.ui.questionbank;

import android.text.TextUtils;

import com.shuoyue.ycgk.entity.Option;
import com.shuoyue.ycgk.entity.QuestionParent;
import com.shuoyue.ycgk.entity.QuestionSimple;
import com.shuoyue.ycgk.entity.UserAnswer;
import com.shuoyue.ycgk.entity.UserAnswerPager;

import java.util.ArrayList;
import java.util.List;

public class QuestionUtil {
    /**
     * 获取用户答题答案打包
     *
     * @param questionParents
     * @param pagerId
     * @return
     */
    public static UserAnswerPager getUserAnswerPager(List<QuestionParent> questionParents,
                                                     int pagerId, String pagerType) {
        if (questionParents == null) {
            return null;
        }
        UserAnswerPager userAnswerPager = new UserAnswerPager();
        userAnswerPager.setType(pagerType);
        userAnswerPager.setData(new ArrayList<>());
        userAnswerPager.setMemberTestPaperId(pagerId);
        for (QuestionParent questionParent : questionParents) {
            if (questionParent.isSpcePage()) {
                continue;
            }
            for (QuestionSimple questionSimple : questionParent.getQuestionSimpleDTOS()) {
                if (
                        questionSimple.getIsAnswerRight() == 2
                                || questionSimple.getMyChooseAnswer() == null
                                || questionSimple.getMyChooseAnswer().size() == 0) {
                    //跳过未作答的
                    continue;
                }
                UserAnswer userAnswer = new UserAnswer();
                userAnswer.setMemberPaperDetailId(questionSimple.getMemberPaperDetailId());
                userAnswer.setSeconds(questionSimple.getUseTime() / 1000);
                if (questionSimple.getQuestionType() == 1 ||
                        questionSimple.getQuestionType() == 2
                ) {
                    //选择
                    if (!questionSimple.getMyChooseAnswer().contains("?")) {
                        userAnswerPager.setTime(userAnswerPager.getTime() + questionSimple.getUseTime() / 1000);
                        userAnswer.setAnswer(questionSimple.getMyChooseAnswer());
                        userAnswerPager.getData().add(userAnswer);
                    } else {
                        userAnswerPager.setTime(userAnswerPager.getTime() + questionSimple.getUseTime() / 1000);
                        List<String> mAns = new ArrayList<>();
                        mAns.add("NO");
                        userAnswer.setAnswer(mAns);
                        userAnswerPager.getData().add(userAnswer);
                    }
                } else if (questionSimple.getQuestionType() == 3) {
                    //判断
                    List<String> myAnswerUp = new ArrayList<>();
                    for (String myAnswer : questionSimple.getMyChooseAnswer()) {
                        if (myAnswer.equals("A")) {
                            myAnswerUp.add("1");
                        } else if (myAnswer.equals("B")) {
                            myAnswerUp.add("2");
                        } else if (myAnswer.equals("?")) {
                            myAnswerUp.add("NO");
                        }
                    }
                    userAnswerPager.setTime(userAnswerPager.getTime() + questionSimple.getUseTime() / 1000);
                    userAnswer.setAnswer(myAnswerUp);
                    userAnswerPager.getData().add(userAnswer);

                }
            }
        }

        if (userAnswerPager.getData() == null || userAnswerPager.getData().size() == 0) {
            return null;
        }
        return userAnswerPager;
    }


    /**
     * 包装网上拉下来的题目
     * 1.初始化参数同base的encode
     * 2.初始化已有答案
     *
     * @param questionParents
     * @return
     */
    public static List<QuestionParent> repackQuestionParent(List<QuestionParent> questionParents,
                                                            boolean isShowRight) {

        encode(questionParents, isShowRight);
        return questionParents;
    }

    public static void encode(List<QuestionParent> questionParents, boolean isShowRight) {
        int now = 1;
        for (int i = 0; i < questionParents.size(); i++) {
            QuestionParent questionParent = questionParents.get(i);
            //跳过过渡页
            if (questionParent.isSpcePage()) {
                continue;
            }

            if (questionParent.getIsMaterial() == 1) {
                //材料题,编号
                for (QuestionSimple questionSimple : questionParent.getQuestionSimpleDTOS()) {
                    questionSimple.setParentPos(i);
                    questionSimple.setNum(now);
                    now++;
                    swipMyAnswer(questionSimple, isShowRight);
                    //设置选项是否正确
                    if (questionSimple.getQuestionSimpleOptionDTOS() != null)
                        for (Option o : questionSimple.getQuestionSimpleOptionDTOS()) {
                            if (questionSimple.getRightKey().contains(o.getNo())) {
                                o.setRight(true);
                            }
                        }
                    //判断题需要自己加选项
                    if (questionSimple.getQuestionType() == 3) {
                        questionSimple.setRightKey("1".equals(questionSimple.getRightKey())
                                ? "A" : "B");
                        if (questionSimple.getAnswer() != null && questionSimple.getAnswer().equals("1")) {
                            questionSimple.setAnswer("A");
                        } else if (questionSimple.getAnswer() != null && questionSimple.getAnswer().equals("2")) {
                            questionSimple.setAnswer("B");
                        }
                        ArrayList<Option> options = new ArrayList<>();
                        Option option1 = new Option("A", "正确", "A".equals(questionSimple.getRightKey()), !TextUtils.isEmpty(questionSimple.getAnswer()) &&
                                questionSimple.getAnswer().equals("A"));
                        Option option2 = new Option("B", "错误", "B".equals(questionSimple.getRightKey()), !TextUtils.isEmpty(questionSimple.getAnswer()) &&
                                questionSimple.getAnswer().equals("B"));
                        options.add(option1);
                        options.add(option2);
                        questionSimple.setQuestionSimpleOptionDTOS(options);
                    }
                }
            } else {
                QuestionSimple questionSimple = questionParent.getQuestionSimpleDTOS().get(0);
                questionSimple.setParentPos(i);
                questionSimple.setNum(now);
                now++;


                //设置选项是否正确
                if (questionSimple.getQuestionSimpleOptionDTOS() != null)
                    for (Option o : questionSimple.getQuestionSimpleOptionDTOS()) {
                        if (questionSimple.getRightKey().contains(o.getNo())) {
                            o.setRight(true);
                        }
                        if (questionSimple.getAnswer() != null &&
                                questionSimple.getAnswer().contains(o.getNo())) {
                            o.setSelect(true);
                        }
                    }

                //判断题需要自己加选项
                if (questionSimple.getQuestionType() == 3) {
                    questionSimple.setRightKey("1".equals(questionSimple.getRightKey())
                            ? "A" : "B");
                    if (questionSimple.getAnswer() != null && questionSimple.getAnswer().equals("1")) {
                        questionSimple.setAnswer("A");
                    } else if (questionSimple.getAnswer() != null && questionSimple.getAnswer().equals("2")) {
                        questionSimple.setAnswer("B");
                    }
                    ArrayList<Option> options = new ArrayList<>();
                    Option option1 = new Option("A", "正确", "A".equals(questionSimple.getRightKey()), !TextUtils.isEmpty(questionSimple.getAnswer()) &&
                            questionSimple.getAnswer().equals("A"));
                    Option option2 = new Option("B", "错误", "B".equals(questionSimple.getRightKey()), !TextUtils.isEmpty(questionSimple.getAnswer()) &&
                            questionSimple.getAnswer().equals("B"));
                    options.add(option1);
                    options.add(option2);
                    questionSimple.setQuestionSimpleOptionDTOS(options);
                }
                swipMyAnswer(questionSimple, isShowRight);
            }
        }

    }

    /**
     * 包装我的答案
     *
     * @param questionSimple
     * @param isShowAsy
     */
    private static void swipMyAnswer(QuestionSimple questionSimple, boolean isShowAsy) {
        if (!TextUtils.isEmpty(questionSimple.getAnswer())) {
            ArrayList<String> strings = new ArrayList<>();
            String[] ss = questionSimple.getAnswer().split(",");
            for (String si : ss) {
                strings.add(si);
            }
            questionSimple.setMyChooseAnswer(strings);
            if (questionSimple.getQuestionType() == 3) {
                questionSimple.setIsAnswerRight(
                        questionSimple.getAnswer().equals("1")
                                && questionSimple.getRightKey().equals("A")
                                ||
                                questionSimple.getAnswer().equals("2")
                                        && questionSimple.getRightKey().equals("B")
                                ? 1 : 0);
            } else {
                questionSimple.setIsAnswerRight(
                        questionSimple.getRightKey().equals(questionSimple.getAnswer()) ? 1 : 0
                );
            }
            if (isShowAsy) {
                questionSimple.setShowRight(true);
            }
        }
    }

}
